import functools

import grpc
from proto import user_pb2_grpc, user_pb2


def run(func):
    def wrapper(*args, **kwargs):
        with grpc.insecure_channel('localhost:10059') as channel:
            stub = user_pb2_grpc.UserServiceStub(channel)
            return func(stub)
    return wrapper


@run
def user_list(stub):
    response = stub.UserList(user_pb2.Page(page=2, pageSize=3))
    print(response.total)
    for u in response.data:
        print('id: ', u.id, ' ', 'name: ', u.name, '  ', 'gender: ', u.gender)
    return response


@run
def register(stub):
    response = stub.Register(user_pb2.UserRegisterRequest(
        name='七七', gender='female', password='123456'
    ))
    print(response)


if __name__ == '__main__':
    # register()
    user_list()
